<template>
    <div>
        <Modal
                v-model="modal"
                title="确认操作"
                width="20"
                @on-ok="handleSubmit">
            <div style="text-align:center">
                <p>是否确认保存!</p>
            </div>
        </Modal>
        <Button v-if="showBackBtn" class="goBack" type="primary" @click="$router.back()">
            <Icon type="ios-arrow-back"/>
            返回
        </Button>
        <Card class="line-style" v-for="(item,index) in streetList" :key="item.dict_id">
            <div class="line-wrap">
                <div class="line-title" style="flex-direction:column;justify-content:center">
                    <p>{{item.name}}</p>
                </div>
                <div class="line-main">
                    <i-form :ref="'streetList'+item.dict_id" :model="item" :rules="formRules" :label-width="120">
                        <Row>
                            <Col span="7" >
                                <FormItem label="企业复工数量" prop="resumption_count">
                                    <Input type="number" number v-model="item.resumption_count" placeholder="请输入企业复工数量">
                                    </Input>
                                </FormItem>
                            </Col>
                            <Col span="7" >
                                <FormItem label="企业复工率" prop="resumption_ratio">
                                    <Input type="number" number v-model="item.resumption_ratio" placeholder="请输入企业复工率">
                                        <span slot="append">%</span>
                                    </Input>
                                </FormItem>
                            </Col>
                            <Col span="7">
                                <FormItem label="上报日期" prop="imp_date">
                                    <DatePicker type="date" :options="options1" @on-change="dateChange($event,index)"  placeholder="请选择上报日期"
                                                :value="item.imp_date" format="yyyy-MM-dd"></DatePicker>
                                </FormItem>
                            </Col>
                        </Row>
                    </i-form>
                    <div class="line-button" v-if="!showDetail">
                        <Button type="primary" @click="validateForm('streetList'+item.dict_id,index)">保存</Button>
                    </div>
                </div>
            </div>
        </Card>

    </div>
</template>

<script>
import { getDictInfoList, insert, findList, update } from '@/api/outbreak-management/resume-work/enterprise'

// 企业复工复产
export default {
  data () {
    const validateInt = (rule, value, callback) => {
      if (value % 1 !== 0) {
        callback(new Error('请输入整数'))
      } else {
        callback()
      }
    }
    const validateDouble = (rule, value, callback) => {
      const reg = /^(0|\+?[1-9][0-9]{0,8})(\.\d{1,2})?$/
      if (!reg.test(value)) {
        callback(new Error('最多只允许两位小数'))
      } else {
        callback()
      }
    }
    return {
      options1: { // 不可选今天之后的日期
        disabledDate (date) {
          return date && date.valueOf() > Date.now()
        }
      },
      streetList: [],
      // 保存状态码
      state: '',
      // 提示框
      modal: false,
      // 控制保存按钮显隐
      showDetail: false,
      // 控制返回按钮显隐
      showBackBtn: false,
      // 表单规则
      formRules: {
        resumption_count: [
          { required: true, type: 'number', message: '企业复工数量必填', trigger: 'blur' },
          { min: 0, max: 99999999, type: 'number', message: '行业复工数范围0-99999999', trigger: 'blur' },
          { validator: validateInt, trigger: 'blur' }
        ],
        resumption_ratio: [
          { required: true, type: 'number', message: '企业复工率必填', trigger: 'blur' },
          { min: 0, max: 99999999, type: 'number', message: '行业复工数范围0-99999999', trigger: 'blur' },
          { validator: validateDouble, trigger: 'blur' }
        ],
        imp_date: [
          { required: true, message: '上报时间必填', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 日期转字符串
    dateChange (date, index) {
      console.log(this.streetList)
      for (let i = 0; i < this.streetList.length; i++) {
        this.streetList[i].imp_date = date
      }
    },
    // 重置
    handleReset (name) {
      this.$refs[name].resetFields()
    },
    // 校验保存数据
    validateForm (name, index) {
      this.$refs[name][0].validate((valid) => {
        if (valid) {
          this.currentIndex = index
          this.modal = true
        } else {
          this.$Message.error('表单验证失败,请检查后再试!')
        }
      })
    },
    // 保存
    handleSubmit (name) {
      let streetList = this.streetList[this.currentIndex]
      if (streetList.id) {
        let { dict_id, resumption_count, resumption_ratio, imp_date, id } = streetList
        let street = { dict_id: dict_id, resumption_count: resumption_count, resumption_ratio: resumption_ratio, imp_date: imp_date, id: id }
        update(street).then(({ data }) => {
          let { errcode } = data
          if (errcode === 0) {
            this.$Message.success('保存成功!')
            this.modal = false
            this.formType = ''
          } else {
            this.$Message.error('保存失败!')
          }
        })
      } else {
        let { dict_id, resumption_count, resumption_ratio, imp_date } = streetList
        let street = { dict_id: dict_id, resumption_count: resumption_count, resumption_ratio: resumption_ratio, imp_date: imp_date }
        insert(street).then(({ data }) => {
          let { errcode } = data
          if (errcode === 0) {
            this.$Message.success('保存成功!')
            this.modal = false
            this.currentIndex = ''
          } else {
            this.$Message.error('保存失败!')
          }
        })
      }
    },
    // 获取企业字典
    getDictInfoList () {
      getDictInfoList({}).then(({ data }) => {
        let { data: res, errcode } = data
        if (errcode === 0 && res.length > 0) {
          let streetList = []
          res.forEach(item => {
            let { label, value } = item
            let formObj = {
              dict_id: value,
              name: label,
              resumption_count: 0,
              resumption_ratio: 0,
              imp_date: ''
            }
            streetList.push(formObj)
          })
          this.streetList = streetList
        }
      })
    },
    // 修改状态
    isEditStatus () {
      if (this.$route.query && this.$route.query.imp_date) {
        if (this.$route.query.showDetail) {
          this.showDetail = true
        }
        this.showBackBtn = true
        this.getEditData(this.$route.query.imp_date)
      } else {
        this.getDictInfoList()
      }
    },
    getEditData (imp_date) {
      findList({ imp_date }).then(({ data }) => {
        let { errcode, data: res } = data
        if (errcode === 0) {
          res.forEach(item => {
            item.resumption_count = +item.resumption_count
            item.resumption_ratio = +item.resumption_ratio
            item.imp_date = this.$common.moment(item.imp_date).format('YYYY-MM-DD')
          })
        }
        this.streetList = res
      })
    }
  },
  created () {
    this.isEditStatus()
  },
  mounted () {
  }
}
</script>
<style scoped>

</style>
